package com.hospital.controller;

import com.hospital.entity.FollowUpGroup;
import com.hospital.entity.Patient;
import com.hospital.service.PatientService;
import com.hospital.util.JsonUtil;
import com.hospital.util.PagingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 患者操作Controller
 */
@Controller
@RequestMapping("Patient")
@ResponseBody
public class PatientController {
    @Autowired
    private PatientService patientService;
    @Autowired
    private JsonUtil jsonUtil;

    /**
     * 查所有随访组对应的患者
     *
     * @param page    当前页
     * @param pageNum 每页显示条数
     * @param status  随访状态
     * @return 集合中的患者信息
     */
    @RequestMapping("SelectAll")
    public List<Object> SelectAll(Integer page, Integer pageNum, String status) {
        List<Object> list = new ArrayList<>();
        Integer count = patientService.SelectAllCounts(status);
        Map<String, Integer> paging = PagingUtil.paging(page, pageNum, count);
        Integer pages = paging.get("page");
        Integer pageNums = paging.get("pageNum");
        List<Patient> patients = patientService.SelectAll(pages, pageNums, status);
        list.add(patients);
        list.add(paging);
        return list;
    }

    /**
     * 条件查询数量
     *
     * @param page          当前页
     * @param pageNum       每页显示条数
     * @param followUpGroup 随访组对象
     * @param patient       患者对象
     * @return 集合中的条件查询信息
     */
    @RequestMapping("SelectLike")
    public List<Object> SelectAll(Integer page, Integer pageNum, FollowUpGroup followUpGroup, Patient patient) {
        List<Object> list = new ArrayList<>();
        //名字模糊查询
        patient.setFullName("%" + patient.getFullName() + "%");
        //返回的数量
        Integer count = patientService.SelectLikeCount(followUpGroup, patient);
        //分也工具类
        Map<String, Integer> paging = PagingUtil.paging(page, pageNum, count);
        Integer pages = paging.get("page");
        Integer pageNums = paging.get("pageNum");
        //条件查询的数据
        List<Patient> patients = patientService.SelectLike(pages, pageNums, followUpGroup, patient);
        list.add(paging);
        list.add(patients);
        return list;
    }

    /**
     * 查一个患者信息
     *
     * @param id 患者primary key
     * @return 患者信息
     */
    @RequestMapping("SelectById")
    public Patient SelectById(Integer id) {
        Patient patient = patientService.SelectById(id);
        return patient;
    }

    /**
     * 修改患者信息
     *
     * @param patient 患者对象
     * @return 修改状态
     */
    @RequestMapping("Update")
    public Boolean Update(Patient patient) {
        Integer state = patientService.Update(patient);
        jsonUtil.setRestate(state == 1);
        return jsonUtil.getRestate();
    }

    /**
     * 删除患者方法
     *
     * @param id 患者id
     * @return 删除状态
     */
    @RequestMapping("Delete")
    public Boolean Delete(Integer id) {
        Integer state = patientService.Delete(id);
        jsonUtil.setRestate(state == 1);
        return jsonUtil.getRestate();
    }
}
